package router

import (
	"coll-back/handlers"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
func Init() *gin.Engine {
	r := gin.Default()
	r.Use(Cors())
	r.POST("/login", handlers.Login)
	r.GET("/list", handlers.List)
	r.GET("/detail", handlers.Detail)
	/////////////////////////15.2//////////////////////////
	var order = r.Group("/order")
	order.POST("/login", handlers.LoginUser)
	order.POST("/create", handlers.CreateUserNode)
	order.GET("/listUserRole", handlers.ListUserRole)
	order.GET("/listRoleNode", handlers.ListRoleNode)
	order.POST("/createOrder", handlers.CreateOrder)
	order.POST("/createDepotOrder", handlers.CreateDepotOrder)
	order.GET("/listOrder", handlers.ListOrder)
	order.GET("/detailOrder", handlers.DetailOrder)
	return r
}
